<template>
    <div class="main-container">
        <div class="main-content">
            <div class="row">
                <div class="col-sm-10 col-sm-offset-1">
                    <div class="login-container">
                        <div class="text-center">
                            <h1>
                                <i class="ace-icon fa fa-leaf green"></i>
                                <span class="black">在线视频系统登录</span>
                            </h1>
                        </div>

                        <div class="space-6"></div>

                        <div class="position-relative">
                            <div id="login-box" class="login-box visible widget-box no-border">
                                <div class="widget-body">
                                    <div class="widget-main">
                                        <h4 class="header blue lighter bigger">
                                            <i class="ace-icon fa fa-coffee green"></i>
                                            请输入用户登录信息
                                        </h4>

                                        <div class="space-6"></div>

                                        <form>
                                            <fieldset>
                                                <label class="block clearfix">
														<span class="block input-icon input-icon-right">
															<input type="text" v-model="user.loginName" class="form-control" placeholder="用户名" />
															<i class="ace-icon fa fa-user"></i>
														</span>
                                                </label>

                                                <label class="block clearfix">
														<span class="block input-icon input-icon-right">
															<input type="password"  v-model="user.password" class="form-control" placeholder="密码" />
															<i class="ace-icon fa fa-lock"></i>
														</span>
                                                </label>

                                                <div class="space"></div>

                                                <div class="clearfix">
                                                    <label class="inline">
                                                        <input type="checkbox" v-model="rememberMe" class="ace" />
                                                        <span class="lbl"> 记住我 </span>
                                                    </label>

                                                    <button @click="login()" type="button" class="width-35 pull-right btn btn-sm btn-primary">
                                                        <i class="ace-icon fa fa-key"></i>
                                                        <span class="bigger-110">登录</span>
                                                    </button>
                                                </div>

                                                <div class="space-4"></div>
                                            </fieldset>
                                        </form>

                                        <div class="social-or-login text-center">
                                            <span class="bigger-110">Or Login Using</span>
                                        </div>

                                        <div class="space-6"></div>

                                        <div class="social-login text-center">
                                            <a class="btn btn-primary">
                                                <i class="ace-icon fa fa-facebook"></i>
                                            </a>

                                            <a class="btn btn-info">
                                                <i class="ace-icon fa fa-twitter"></i>
                                            </a>

                                            <a class="btn btn-danger">
                                                <i class="ace-icon fa fa-google-plus"></i>
                                            </a>
                                        </div>
                                    </div><!-- /.widget-main -->
                                </div><!-- /.widget-body -->
                            </div><!-- /.login-box -->
                        </div><!-- /.position-relative -->
                    </div>
                </div><!-- /.col -->
            </div><!-- /.row -->
        </div><!-- /.main-content -->
    </div><!-- /.main-container -->
</template>
<script>
    export default {
        name: 'login',
        mounted() {//在组件内容被渲染到页面之【后】立即执行的函数
            let _this = this;
            $("body").removeClass("no-skin");
            $('body').attr('class', 'login-layout light-login');
            //从本地缓冲中读取用户信息
            let rememberUser = LocalStorage.get(LOCAL_KEY_REMEMBER_USER);
            if(rememberUser){
                _this.user = rememberUser;
            }
        },
        data:function(){
            return {
                user: {},
                rememberMe: true,
            }
        },
        methods:{
            login(){
                let _this = this;
                //从本地缓冲中读取用户信息
                let newUser = LocalStorage.get(LOCAL_KEY_REMEMBER_USER);
                //定义临时变量接收加密后的密码
                let md5Password = _this.user.password ;
                //输入的两次密码有区别
                if(Tool.isEmpty(newUser) || (md5Password !== newUser.password)){
                    _this.user.password = hex_md5(_this.user.password + KEY);//前端进行一次加密
                }
                _this.$ajax.post(process.env.VUE_APP_SERVER+'/system/user/login', _this.user).then((response)=>{
                    let resp = response.data;
                    if (resp.success) {
                        //将用户名密码存储
                        let loginUser = resp.loginUser;
                        //存储token
                        Tool.setLoginUser(loginUser);
                        //判断是否选中记住密码
                        if(_this.rememberMe){
                            loginUser.password =  _this.user.password;
                            LocalStorage.set(LOCAL_KEY_REMEMBER_USER, loginUser);
                        }else{
                            LocalStorage.set(LOCAL_KEY_REMEMBER_USER,null);//清空存储的用户名和密码
                        }
                        //默认跳转到admin组件
                        this.$router.push("/home")
                    } else {
                        Toast.warning(resp.msg)
                    }
                });

            }
        }
    }
</script>
<style>

</style>

